package com.ywwxhz.app.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.view.View;

import com.ywwxhz.Util.Config;
import com.ywwxhz.plugins.battery.BatteryModel;
import com.ywwxhz.plugins.pkgmgr.app.MainActivity;
import com.ywwxhz.plugins.tasks.TaskModel;
import com.ywwxhz.service.CoreService;
import com.ywwxhz.taskmgr.R;

public class PluginsPreferenceFragment extends BasePreferenceFragment {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_plugins);
    }

    @Override
    protected int setSubtitle() {
        return R.string.pref_plugins_title;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        findPreference(getString(R.string.pref_autokill_key)).setOnPreferenceChangeListener(this);
        findPreference(getString(R.string.pref_lockscreen_key)).setOnPreferenceChangeListener(this);
        findPreference(getString(R.string.pref_batteryline_key)).setOnPreferenceChangeListener(this);
        findPreference(getString(R.string.pref_packagemgr_key)).setOnPreferenceClickListener(this);
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        Intent autoKillService = new Intent(getActivity(), CoreService.class);
        if (preference.getKey().equals(getString(R.string.pref_autokill_key))) {
            autoKillService.putExtra(Config.SERVICE_PARAM, TaskModel.MODEL);
            if ((Boolean) newValue) {
                autoKillService.putExtra(TaskModel.PARAM,
                        TaskModel.AUTO_KILL_ENABLE);
            } else {
                autoKillService.putExtra(TaskModel.PARAM,
                        TaskModel.AUTO_KILL_DISABLE);
            }
        } else if (preference.getKey().equals(getString(R.string.pref_lockscreen_key))) {
            autoKillService.putExtra(Config.SERVICE_PARAM, TaskModel.MODEL);
            if ((Boolean) newValue) {
                autoKillService.putExtra(TaskModel.PARAM,
                        TaskModel.LOCK_SCREEN_ENABLE);
            } else {
                autoKillService.putExtra(TaskModel.PARAM,
                        TaskModel.LOCK_SCREEN_DISABLE);
            }
        } else if (preference.getKey().equals(getString(R.string.pref_batteryline_key))) {
            autoKillService.putExtra(Config.SERVICE_PARAM, BatteryModel.MODEL);
            if ((Boolean) newValue) {
                autoKillService.putExtra(BatteryModel.PARAM,
                        BatteryModel.LINE_ENABLE);
            } else {
                autoKillService.putExtra(BatteryModel.PARAM,
                        BatteryModel.LINE_DISABLE);
            }
        } else {
            return false;
        }
        getActivity().startService(autoKillService);
        return true;
    }

    @Override
    public boolean getShouldExit() {
        return true;
    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
        if (preference.getKey().equals(getString(R.string.pref_packagemgr_key))) {
            Intent intent = new Intent(getActivity(), MainActivity.class);
            getActivity().startActivity(intent);
        }
        return false;
    }
}